#include "set_wifi.h"
set_wifi setwifi;



bool set_wifi::smart_conect(u8 mode)
{
    s8 x;
    ui.set_cpu_freq(HIGH_FREQ);
    for(x=-96;x<17;x+=8)
    {
        oled.clr();
        oled.chinese(x,0,"请连接至热点",16,1,0);
        oled.str(x+4,20,"QF_Maker_AP",16,1,0);
        oled.chinese(x+16,40,"进行配置",16,1,0);
        oled.refresh();
    }
    wifima.work_mode_set(mode);
    if(wifima.startConfigPortal("QF_Maker_AP"))
    {
      // if(!mode )
      //   ESP.reset();
      oled.roll(0,0,128,8,8,RIGHT,16);
      return 1;
    }
    else
    {
      oled.roll(0,0,128,8,8,RIGHT,16);
      return 0;
    }
    
}

u8 set_wifi::get_location()
{
    if(location_mode)
    {
        if((longitude == ""||latitude == "")&&location_text == "")
        {
            location_mode = 0;
            eeprom.write(location_add,location_mode);
            location_text_buf = "";
        }
        else
        {
          if(location_text_buf == "")
          {
            if(location_text == "")
            {
              location_text_buf = longitude + "," + latitude;
            }
            else
            {
              if(location_up_text != "")
                location_text_buf = location_text + "&adm=" + location_up_text;
              else
                location_text_buf = location_text;
            }
          }
          return 1;
        }
    }


    if(location_text_buf != "")
        return 1;
    else
    {
      if(iplocation.get())
      {
        location_text_buf = iplocation.longitude + "," + iplocation.latitude;
        return 1;
      }
      return 0;
    }

}


void set_wifi::power_on_conect()
{
  u8 key_buf;
  short x;
  oled.clr();
  x = -(16*5);
  ui.set_cpu_freq(HIGH_FREQ);
  while(x<12)
  {
    oled.choose_clr(0,2,128,2);
    oled.display_parameters(x+=2,16,16,1,0);
    oled.chinese("网络连接中");
    oled.str("...");
    oled.choose_refresh(0,2,128,2);
    yield();
  }
  if(!wifima.conect_last())
  {
    for(x=0;x<129;x+=8)
    {
      oled.clr();
      oled.display_parameters(x+12,10);
      oled.chinese("网络连接中");
      oled.str("...");

      oled.str(0+(x-128),0,"NO",16,1,0);
      oled.str(102+(x-128),0,"YES",16,1,0);
      oled.display_parameters(16+(x-128),28);
      oled.chinese("网络连接失败");
      oled.display_parameters(16+(x-128),48);
      oled.chinese("是否进行配置");
      oled.refresh();
      yield();
    }
  
    while(1)
    {
      key_buf=key.get_once();
      if(key_buf==RIGHT)
      {
        oled.roll(0,0,128,8,8,RIGHT,16);
        if(!smart_conect(config_network))
          break;
        ESP.reset();
      }
      else if(key_buf==LEFT)
      {
        fail_count = 4;
        oled.roll(0,0,128,8,8,RIGHT,16);
        break;
      }     
      delay(25);
    }
  }
  else
  {
    for(x=0;x<128;x+=2)
    {
      oled.choose_clr(0,2,128,2);
      oled.display_parameters(x+12,16);
      oled.chinese("网络连接中");
      oled.str("...");
      oled.chinese(16+(x-128),16,"正在同步位置",CH_SIZE_16,WHITE,NOT_LINE_FEED);
      oled.choose_refresh(0,2,128,2);
      yield();
    }
    if(get_location())
    {
      if(hweather.get_city(location_text_buf))
      {
        for(x=0;x<128;x+=2)
        {
          oled.choose_clr(0,2,128,2);
          oled.chinese(16+x,16,"正在同步位置",CH_SIZE_16,WHITE,NOT_LINE_FEED);
          oled.str(24+(x-128),16,hweather.location,FONT_SIZE_16,WHITE,NOT_LINE_FEED);
          oled.choose_refresh(0,2,128,2);
          yield();
        }
        delay(2000);
        for(x=0;x<128;x+=2)
        {
          oled.choose_clr(0,2,128,2);
          oled.str(24+x,16,hweather.location,FONT_SIZE_16,WHITE,NOT_LINE_FEED);
          oled.chinese(16+(x-128),16,"正在同步天气",CH_SIZE_16,WHITE,NOT_LINE_FEED);
          oled.choose_refresh(0,2,128,2);
          yield();
        }
        if(hweather.get_weather(hweather.locationID))
        {
          hweather.get_future_weather(hweather.locationID);
          for(x=0;x<128;x+=2)
          {
            oled.choose_clr(0,2,128,2);
            oled.chinese(16+x,16,"正在同步天气",CH_SIZE_16,WHITE,NOT_LINE_FEED);
            oled.chinese(16+(x-128),16,"正在同步时间",CH_SIZE_16,WHITE,NOT_LINE_FEED);
            oled.choose_refresh(0,2,128,2);
            yield();
          }
          if(ntime.get())
          {
            for(x=0;x<128;x+=2)
            {
              oled.choose_clr(0,2,128,2);
              oled.chinese(16+x,16,"正在同步时间",CH_SIZE_16,WHITE,NOT_LINE_FEED);
              oled.choose_refresh(0,2,128,2);
              yield();
            }
            ui.set_cpu_freq(LOW_FREQ);
            pcf.write_day(ntime.year,ntime.month,ntime.day,ntime.week);
            pcf.write_time(ntime.hour,ntime.min,ntime.second);
          }
          else
          {
            for(x=0;x<128;x+=2)
            {
              oled.choose_clr(0,2,128,2);
              oled.chinese(16+x,16,"正在同步时间",CH_SIZE_16,WHITE,NOT_LINE_FEED);
              oled.chinese(16+(x-128),16,"时间同步失败",CH_SIZE_16,WHITE,NOT_LINE_FEED);
              oled.choose_refresh(0,2,128,2);
              yield();
            }
            delay(1000);
            oled.roll(0,2,128,2,2,LEFT,64);
          }
        }
        else
        {
          for(x=0;x<128;x+=2)
          {
            oled.choose_clr(0,2,128,2);
            oled.chinese(16+x,16,"正在同步天气",CH_SIZE_16,WHITE,NOT_LINE_FEED);
            oled.chinese(16+(x-128),16,"天气同步失败",CH_SIZE_16,WHITE,NOT_LINE_FEED);
            oled.choose_refresh(0,2,128,2);
            yield();
          }
          delay(2000);
          oled.roll(0,2,128,2,2,LEFT,64);
        }
      } 
      else
      {
        for(x=0;x<128;x+=2)
        {
          oled.choose_clr(0,2,128,2);
          oled.chinese(16+x,16,"正在同步位置",CH_SIZE_16,WHITE,NOT_LINE_FEED);
          oled.chinese(16+(x-128),16,"位置同步失败",CH_SIZE_16,WHITE,NOT_LINE_FEED);
          oled.choose_refresh(0,2,128,2);
          yield();
        }
        delay(2000);
        oled.roll(0,2,128,2,2,LEFT,64);
      }
    }
    else
    {
      for(x=0;x<128;x+=2)
      {
        oled.choose_clr(0,2,128,2);
        oled.chinese(16+x,16,"正在同步位置",CH_SIZE_16,WHITE,NOT_LINE_FEED);
        oled.chinese(16+(x-128),16,"位置同步失败",CH_SIZE_16,WHITE,NOT_LINE_FEED);
        oled.choose_refresh(0,2,128,2);
        yield();
      }
      delay(2000);
      oled.roll(0,2,128,2,2,LEFT,64);
    }
  }
  ui.set_cpu_freq(LOW_FREQ);
  pcf.read_day();
  ui.data_refresh_move_in();
  ui.show_icon_in();
  dat.low_power_set();
  pinMode(12,INPUT);
  pinMode(16,INPUT);
}